/**
* @author zqp
* @version2017年12月23日 
* @description 
*/
package com.zqp.security.core.authentication.mobile;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
* @author zqp
* @version2017年12月23日 
* @description 
*/
public class SmsCodeAuthenticationProvider implements AuthenticationProvider {
	
	private UserDetailsService userDetailsService;

	/* (non-Javadoc)
	 * @see org.springframework.security.authentication.AuthenticationProvider#authenticate(org.springframework.security.core.Authentication)
	 */
	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		SmsCodeAuthenticationToken token = (SmsCodeAuthenticationToken)authentication;
		UserDetails user  = userDetailsService.loadUserByUsername((String) token.getPrincipal());

		if(user == null) {
			throw new InternalAuthenticationServiceException("无法获取用户信息!");
			
		}
		SmsCodeAuthenticationToken result = new SmsCodeAuthenticationToken(user, user.getAuthorities());
		result.setDetails(token.getDetails());
		return result;

	}

	/* (non-Javadoc)
	 * @see org.springframework.security.authentication.AuthenticationProvider#supports(java.lang.Class)
	 *判断是不是你这种类型
	 */
	@Override
	public boolean supports(Class<?> authentication) {

		return SmsCodeAuthenticationToken.class.isAssignableFrom(authentication);

	}

	public UserDetailsService getUserDetailsService() {
		return userDetailsService;
	}

	public void setUserDetailsService(UserDetailsService userDetailsService) {
		this.userDetailsService = userDetailsService;
	}

}
